package engine.newUi
{
	import engine.GameData;
	import engine.control.ControlCreater;
	import engine.control.loading.MultLoader;
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.data.ResPath;
	import engine.logic.tips.TipsManage;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.tip.TipsMainLogic;
	import engine.ui.AlertUI;
	import engine.ui.UIManage;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.system.ApplicationDomain;
	import flash.text.TextField;
	import flash.utils.Timer;
	
	import newx.StageInfo;
	import newx.com.config.StaticIni;
	import newx.com.control.window.NxWindowEvent;
	
	
	/**
	 * 龙女出谷 Logic
	 * @author wangzj
	 * 
	 */	
	public class DragonGoPlayLogic
	{
		private const m_gril_stutas_jump:String    = "AgainStart";                 //龙女的跳状态
		private const m_gril_stutas_run:String     = "FightRun" ;           //龙女走
		private const m_gril_stutas_runStop:String = "StandOk" ;           //龙女走
		private const m_gril_stutas_stand:String   = "waitStartok";               //龙女站立（侧面）
		private const m_gril_stutas_wait:String    = "waitStart";                //龙女等待（正面）
		private const m_gril_stutas_trace:String   = "StandStart";             //龙女落地
		private const m_gril_stutas_flyup:String   = "UpFlyStart";                 //龙女向上飞得状态
		private const m_gril_stutas_flydown:String = "DownStart";           //龙女向下飞得状态
		private const m_gril_start_x:int           = 270;                  //初始是龙女的x坐标
		private const m_gril_start_y:int           = 430;                  //初始是龙女的y坐标
		
		private static var _self	:DragonGoPlayLogic; //单例
		
		public var m_dragonGoPlay:DragonGoPlayUI;     //界面
		private var m_gameShowbtn:Boolean;         //true显示 false不显示
		private var m_gameShowbtn1:Boolean;         //true显示 false不显示
		
		private var m_sprite:Sprite;                                     //虚拟背景
		private var m_sourceUrlArray:Array;                              //游戏资源地址
		private var m_gameBg:MovieClip;                                  //游戏背景
		private var m_drawrect:Sprite
		private var m_stone_one:MovieClip;                             //石头一
		private var m_stone_two:MovieClip;                             //石头二有树
		private var m_stone_four:MovieClip;                             //石头二有树
		private var m_stone_three:MovieClip;                             //地面
		private var m_stone_finish:MovieClip;                             //地面
		private var m_start_Katoon:MovieClip;
		private var m_gameBgMove:MovieClip;                             //移动背景
		private var m_gameGateObj:Object;
		private var m_flag:int;
		private var m_stone_array:Array;
		
		private var m_bellArray:Array;                                   //铃铛数组
		private const m_stone_move:int = 1;                             //运动中的石头
		private const m_stone_stop:int = 2;                             //石头停止
		private var m_gameNumStone:int;                                 //游戏中已经有最多同时存在两个石头
		private var m_grilHitStoneNum:int;                              //龙女碰撞的石头数
		
		private var m_gril:MovieClip;                                    //龙女
		private var m_mouseEnble:Boolean;                                //鼠标点击能力
		private var m_gril_Runing:Boolean;                               //龙女有没有开始跑
		private var m_gril_standStart:Boolean;                           //龙女待机开始
		private var m_gril_Up:Boolean;                                   //龙女向上
		private var m_gril_down:Boolean;                                 //龙女向下
		private var m_gril_jump:Boolean;                                 //龙女向下
		private var m_gril_Trace:Boolean;                                 //龙女着陆
		private var m_oldTime:int;                                      //运动的时间
		private var m_girl_current_y:int;                               //小球y轴坐标
		private var m_girl_current_status:int;                           //龙女当前状态
		private var m_girl_current_action:int;                           //龙女当前动作
		private var m_girl_old_y:int                                    //旧位置
		private var m_girl_new_y:int                                    //新位置
		private var m_bg_walk:int;                                      //背景走的多长
		private const m_girl_onground_speed:int  = 5;                  //小球在地面上平均速度
		private const m_girl_onsky_speed:int     = 8;                  //在空中走的段数
		private const m_girl_higher:int          = 320;                 //龙女最高度
		private const m_girl_lower:int           = 450;                 //龙女最高度
		private const m_girl_on_ground:int       = 1;                   //龙女在地面状态
		private const m_girl_up_sky:int          = 2;                   //龙女向上的状态
		private const m_girl_jump:int            = 3;                   //龙女跳得状态；
		private const m_girl_uping:int            = 4;                   //龙女上
		
		private var m_timeEvent:Timer;                                   //计时器
		private var m_restarGame:Boolean;                                //重启游戏开罐器
		private var m_beginGameBtn:Boolean;                              //开始按钮
		private var bindIsMiniGameStepIndex:int = -1;						//
		private var bindIsMiniGameGateIndex:int = -1;						//
		private var bindIsMiniGameOverTime:int = -1;
		private var bindIsMiniGameIsStarted:int = -1;
		
		private var m_currentGate:int;                                   //当前关数
		private var m_currentStoneNum:int;                               //当前关数种碰撞的铃铛数
		private var m_currentGateTime:int;                               //当前关素的时间
		
		private var m_gateArray:Array;
		private var m_listArray:Array;
		private var m_gateBoo:Boolean;
		private var m_listBoo:Boolean;
		
		private var m_showClip:MovieClip;                                //界面中游戏背景
		private var m_heightest:int;                                     //最高
		
		private const m_stoneArea_1:Array = [30,126];                    //石头的区域1
		private const m_stoneArea_2:Array = [126,222];
		private const m_stoneArea_3:Array = [222,318];
		private const m_stoneArea_4:Array = [318,414];
		private const m_stoneArea_5:Array = [414,550];
		
		private var     m_stoneProduceDifficult:String;
		private const   m_stoneProduceEasy:String     = "esay";           //容易
		private const   m_stoneProducesimplame:String = "simpleness";    //简单那
		private const   m_stoneProduceHand:String     = "hand";           //困难
		private var     m_createTime:int;                                  //生成石头的间隔
		
		private var   m_stoneAreaObjeasy:Object;                              //石头的掉落位置
		private var   m_stoneAreaObjsimp:Object;                              //石头的掉落位置
		private var   m_stoneAreaObjhand:Object;                              //石头的掉落位置
		
		private var   m_stoneIndex:int;                                     //最后一个石头的位置
		private var   m_lastStoneArea:String;                               //最后一个石头所在的区域
		private var   m_gameFinishMv:Boolean;                               //gameover动画
		private var   m_allStoneFlag:Boolean;                               //判断当前场景中是否有石头
		
		private var  m_gameoverShow:Boolean;                                //游戏结束石头显示
		private var  m_gameoverKatoon:MovieClip                             //结束动画
		private var  m_gameoverBool:Boolean                             //结束动画
		
		private var  m_buttonReward:int;                               //一件奖励值
		private var m_needHeight:int;
		private var m_startKaton:Boolean;     //开始播放动画
		private var m_gateKatoon:Boolean;
		private var m_stoneClass:Class;
		private var m_TimeIsRun:Boolean;                            //时间是否再走
		public function DragonGoPlayLogic()
		{
			
		}
		/*******************初始化 界面*********************/
		/**
		 *  单例
		 */		
		public static function getInstance():DragonGoPlayLogic
		{
			if(_self == null)
			{
				_self = new DragonGoPlayLogic();
			}
			return _self;
		}
		/**
		 * 初始化界面
		 * 初始换所有的数据
		 */		
		public function Init():void
		{
			m_dragonGoPlay = DragonGoPlayUI.getInstance();
			m_gameGateObj = {};
			m_stoneAreaObjeasy = {};
			m_stoneAreaObjsimp = {};
			m_stoneAreaObjhand = {};
			m_gameGateObj = StaticIni.getIniObj("DragonGoPlay");
			m_stoneAreaObjeasy = StaticIni.getIniObj("DragonGoPlayRank1");
			m_stoneAreaObjsimp = StaticIni.getIniObj("DragonGoPlayRank2");
			m_stoneAreaObjhand = StaticIni.getIniObj("DragonGoPlayRank3");
			ModelLocator.getInstance().addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_DRAGON_GATE),dragonGate);
			ModelLocator.getInstance().addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_DRAGON_LIST),dragonList);
			ModelLocator.getInstance().addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_DRAGON_PRIZE),dragonPrize);
			ModelLocator.getInstance().addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_SENDSERVERTIME),getServerTime);
		}
		
		/**
		 * 显示界面
		 * 
		 */		
		public function BeforeShow():void
		{
			
			addEvent();        //监听事件
			
			
		}
		/**
		 * 关闭界面
		 * 
		 */		
		public function AfterHide():void
		{
			for( var i:int = 0; i<m_bellArray.length;i++){
				var stone:MovieClip = m_bellArray[i].stone;
				if(m_bellArray[i].flag == m_stone_move){
					m_sprite.removeChild( stone );
					m_bellArray[i].flag = m_stone_stop;
				}
				stone = null;
			}
			removeEvent();  //移除事件
			m_oldTime = 0;
			m_bg_walk = 0;
			m_heightest = 0;
			m_beginGameBtn = false;
			m_dragonGoPlay.beginBtn.visible = true;
			m_sprite.removeChild( m_gril );
			m_sprite.removeChild( m_gameBg );
			m_dragonGoPlay.windowBG.removeChild( m_sprite );
			m_dragonGoPlay.windowBG.removeChild( m_drawrect );
			m_dragonGoPlay.windowBG.removeChild(m_showClip );
			m_bellArray = null;
			m_stone_array = null;
			m_gril = null;
			m_drawrect = null;
			m_gameBg = null;
			m_stone_one = null;
			m_stone_two = null;
			m_stone_three = null;
			m_gameoverKatoon = null;
			m_stone_finish = null;
			m_start_Katoon = null;
			m_gril_Runing = false;
			m_gril_standStart = false;
			m_girl_current_status = 0;
			m_gameBgMove = null;
			m_sprite = null;
			m_showClip = null;
			m_stoneClass = null;
			m_isSourceOk = false;
		}
		
		/**
		 * 监听事件
		 * 
		 */		
		private function addEvent():void
		{
			bindIsMiniGameOverTime = BindManage.getInstance().BindProp(
				ModelLocator.getInstance().scene.Role,"MiniGameOverTime",this,"OverTime");
			bindIsMiniGameIsStarted = BindManage.getInstance().BindProp(
				ModelLocator.getInstance().scene.Role,"MiniGameIsStarted",this,"IsStarted");
			bindIsMiniGameGateIndex = BindManage.getInstance().BindProp(
				ModelLocator.getInstance().scene.Role,"IsMiniGameStep",this,"chageGate");
			bindIsMiniGameStepIndex = BindManage.getInstance().BindProp(
				ModelLocator.getInstance().scene.Role,"IsOneKeyPrize",this,"chageStep");
			
			m_dragonGoPlay.shortBtn.addEventListener(MouseEvent.ROLL_OVER , OnShortover);
			m_dragonGoPlay.shortBtn.addEventListener(MouseEvent.ROLL_OUT , onShortOutHandle);
			m_dragonGoPlay.shortBtn.addEventListener(MouseEvent.CLICK , OnShortClick);
			m_dragonGoPlay.beginBtn.addEventListener(MouseEvent.CLICK , onBeginClick);
			m_dragonGoPlay.windowBG.addEventListener(NxWindowEvent.WINDOW_CLOSE,onClickCloseHandle);
			
		}
		/**
		 * 移除监听事件
		 * 
		 */		
		private function removeEvent():void
		{
			var timer:uint = m_currentGateTime - m_timeEvent.currentCount;
			if( timer <= 0 ){
				m_timeEvent.removeEventListener(TimerEvent.TIMER, onTimerFun);
				m_timeEvent =  null;
			}
			BindManage.getInstance().UnbindProp(ModelLocator.getInstance().scene.Role,"IsMiniGameStep",bindIsMiniGameGateIndex);
			BindManage.getInstance().UnbindProp(ModelLocator.getInstance().scene.Role,"IsOneKeyPrize",bindIsMiniGameStepIndex);
			BindManage.getInstance().UnbindProp(ModelLocator.getInstance().scene.Role,"MiniGameOverTime",bindIsMiniGameOverTime);
			BindManage.getInstance().UnbindProp(ModelLocator.getInstance().scene.Role,"MiniGameIsStarted",bindIsMiniGameIsStarted);
			m_dragonGoPlay.shortBtn.removeEventListener(MouseEvent.CLICK , OnShortClick);
			m_dragonGoPlay.beginBtn.removeEventListener(MouseEvent.CLICK , onBeginClick);
			m_dragonGoPlay.windowBG.removeEventListener(NxWindowEvent.WINDOW_CLOSE,onClickCloseHandle);
			m_gameBg.removeEventListener(MouseEvent.CLICK , ongameViewClick ); 
			m_gameBg.removeEventListener(Event.ENTER_FRAME, onGameEnterFrame ); 
			(m_showClip.getChildByName("btn1") as SimpleButton).removeEventListener(MouseEvent.ROLL_OVER,onBnt1MouseOver);
			(m_showClip.getChildByName("btn2") as SimpleButton).removeEventListener(MouseEvent.ROLL_OVER,onBtn2MouseOver);
			m_dragonGoPlay.shortBtn.removeEventListener(MouseEvent.ROLL_OVER,OnShortover);
			(m_showClip.getChildByName("btn1") as SimpleButton).removeEventListener(MouseEvent.ROLL_OUT,onShortOutHandle);
			(m_showClip.getChildByName("btn2") as SimpleButton).removeEventListener(MouseEvent.ROLL_OUT,onShortOutHandle);
			m_dragonGoPlay.shortBtn.removeEventListener(MouseEvent.ROLL_OUT,onShortOutHandle);
		}
		
		
		/***********************加载资源************************/
		/**
		 * 外部加载资源
		 */		
		private function loadSource() : void
		{
			m_sourceUrlArray = [];
			m_sourceUrlArray.push( ResPath.SWF + "DragonOut.swf" );    //人物
			m_sourceUrlArray.push( ResPath.SWF + "bg.swf" );            //背景
			m_sourceUrlArray.push( ResPath.SWF + "switchScene1.swf" );            //背景
			m_sourceUrlArray.push( ResPath.SWF + "DragonGame.swf" );            //背景
			
			MultLoader.getInstance().load( m_sourceUrlArray , true , gameShow );
		}
		
		
		/**
		 * 对象提取器
		 */		
		private function addChildLoaderSource( stutas:String  ) : MovieClip
		{
			var sourceMovie:MovieClip;
			sourceMovie =MultLoader.getInstance().getSourceByUrl( stutas );
			return sourceMovie as MovieClip;
		}
		
		private function addDomainSource( name:String ) : MovieClip
		{
			var sourceMovie:MovieClip;
			var domain:ApplicationDomain = m_showClip.loaderInfo.loader.contentLoaderInfo.applicationDomain;
			var sClass:Class = domain.getDefinition( name ) as Class;
			if( name == "CustomControl_0208" ){
				m_stoneClass = sClass;
				return null;
			}
			if( sClass ){
				sourceMovie = new sClass();
				return sourceMovie;
			}
			return null;
		}
		
		
		/***********************界面显示逻辑********************/
		/**
		 * 一键奖励
		 * @param event
		 */		
		private function OnShortClick( event:MouseEvent ) : void
		{
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENR_CUSTOMMSG_DRAGON_CLICK);
		}
		/**
		 * 0是没通关 1是通关
		 * @param val
		 */		
		public function set chageGate( val:int ) : void
		{
			if(val){
				m_gameShowbtn = true;
				m_dragonGoPlay.showOrHide();
			}else{
				m_gameShowbtn = false;
			}
			shortBtnFun();
		}
		
		/**
		 * 0 是没领取 1领取
		 * @param val
		 * 一键按钮
		 */		
		public function set chageStep(val:int):void
		{
			if(val){
				m_gameShowbtn1 = true;
				m_dragonGoPlay.beginBtn.enabled = false;
			}else{
				m_gameShowbtn1 = false;
				m_dragonGoPlay.beginBtn.enabled = true;
			}
			
			shortBtnFun();
		}
		
		/**
		 * 游戏结束时间
		 * @param val
		 */		
		private var m_overTime:int;
		private var m_serverTime:int;
		private var m_isStart:Boolean;
		public function set OverTime( val:int) : void
		{
			m_overTime = val;
			
		}
		
		/**
		 * 得到服务器当前时间 
		 * @param e
		 * 
		 */		
		private function getServerTime(e:CustomMsgEvent):void
		{
			var over:int;
			m_serverTime			=	e.pList[0];
			over = m_overTime - m_serverTime;
			if( m_isStart ){
				if( over > 0 ){
					m_currentGateTime   =	over; 
					
					m_dragonGoPlay.beginBtn.visible = false;
					m_gateFlag= 0;
					m_dragonGoPlay.beginBtn.visible = false;
					m_dragonGoPlay.shortBtn.enabled = false;
					m_beginGameBtn = true;
					m_isStart = false;
				}
			}
		}
		/**
		 * 今天是否开始过
		 * @param e
		 * 
		 */		
		public function  set IsStarted( val:int ):void
		{
			if( val ){
				m_isStart = true;
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_QUERYSERVERTIME);
			} 
			
		}
		
		/**
		 * 关于一键奖励按钮
		 * 0 0 都为零时 这是才可以领取
		 */		
		private function shortBtnFun():void
		{
			if(!m_gameShowbtn1&&!m_gameShowbtn)
			{
				m_dragonGoPlay.shortBtn.enabled = true;
			}else{
				m_dragonGoPlay.shortBtn.enabled = false;
			}
		}
		
		/**
		 * 开始按钮
		 * @param event
		 * 
		 */		
		private var m_gateFlag:int;
		private function onBeginClick( event:MouseEvent ) : void
		{
			m_gateFlag= 1;
			m_dragonGoPlay.beginBtn.visible = false;
			m_dragonGoPlay.shortBtn.enabled = false;
			m_beginGameBtn = true;
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_MINI_GAME_BEGIN,m_currentGateTime);
		}
		/**
		 * 关闭界面
		 */		
		private function onClickCloseHandle(e:NxWindowEvent):void
		{
			m_dragonGoPlay.showOrHide();
		}
		
		private function OnShortover(e:MouseEvent):void
		{
			TipsMainLogic.instance.showString(StaticIni.getSwfLangStrVar("UIString_1000584",[ m_buttonReward ]));
		}
		
		private function onBnt1MouseOver(e:MouseEvent):void
		{
			TipsMainLogic.instance.showString(StaticIni.getSwfLangStr("UIString_1000582"));
		}
		
		private function onBtn2MouseOver(e:MouseEvent):void
		{
			TipsMainLogic.instance.showString(StaticIni.getSwfLangStr("UIString_1000583"));
		}
		/**
		 * 关闭TIPs 
		 * @param e
		 * 
		 */	
		private function onShortOutHandle(e:MouseEvent):void
		{
			TipsMainLogic.instance.hide();
		}
		
		/**
		 * 游戏显示
		 * 游戏场景分层
		 */		
		private function gameShow( ) : void
		{
			m_bellArray = [];
			m_stone_array = [];
			m_dragonGoPlay.showOrHide();
			
			m_showClip = addChildLoaderSource(ResPath.SWF + "DragonGame.swf" );
			m_showClip.x = 10;
			m_showClip.y = 40;
			m_dragonGoPlay.windowBG.addChildAt(m_showClip , 1);
			
			(m_showClip.getChildByName("btn1") as SimpleButton).addEventListener(MouseEvent.ROLL_OVER,onBnt1MouseOver);
			(m_showClip.getChildByName("btn2") as SimpleButton).addEventListener(MouseEvent.ROLL_OVER,onBtn2MouseOver);
			(m_showClip.getChildByName("btn1") as SimpleButton).addEventListener(MouseEvent.ROLL_OUT,onShortOutHandle);
			(m_showClip.getChildByName("btn2") as SimpleButton).addEventListener(MouseEvent.ROLL_OUT,onShortOutHandle);
			
			m_dragonGoPlay.lastTimeLab.htmlText = "<font color='#00694A'>" + "00" + "</font>";
			
			m_sprite = new Sprite();     //虚拟区
			m_sprite.x = 232;
			m_sprite.y = 20;
			m_dragonGoPlay.windowBG.addChild( m_sprite  );
			m_dragonGoPlay.windowBG.swapChildren( m_sprite , m_dragonGoPlay.beginBtn  );
			
			
			m_stone_one = addDomainSource("CustomControl_0206");
			m_stone_two = addDomainSource("CustomControl_0207");
			m_stone_four = addDomainSource("CustomControl_0207");
			m_stone_three =addDomainSource("CustomControl_0212");
			m_stone_finish = addDomainSource("CustomControl_0225");
			addDomainSource("CustomControl_0208");
			m_gameBgMove = ControlCreater.getInstance().CreateCustomControl("CustomControl_0213");
			m_start_Katoon = ControlCreater.getInstance().CreateCustomControl("CustomControl_0228");
			m_start_Katoon.name = "m_start_Katoon";
			m_stone_array.push( {"stone":m_stone_three ,"flag":m_stone_stop,"name":"three"});
			m_stone_array.push( {"stone":m_stone_one ,"flag":m_stone_stop,"name":"one"});
			m_stone_array.push( {"stone":m_stone_two ,"flag":m_stone_stop,"name":"two"});
			m_stone_array.push( {"stone":m_stone_four ,"flag":m_stone_stop,"name":"four"});
			
			m_drawrect = new Sprite();
			m_drawrect.graphics.beginFill(0xff0000);
			m_drawrect.graphics.drawRect(0,0,592,443);
			m_drawrect.graphics.endFill();
			m_drawrect.x = 232;
			m_drawrect.y = 76;
			m_sprite.mask = m_drawrect;
			m_dragonGoPlay.windowBG.addChild( m_drawrect );
			
			m_gameBg = addChildLoaderSource( ResPath.SWF + "bg.swf" );
			m_sprite.addChild( m_gameBg );
			m_sprite.addEventListener(MouseEvent.CLICK , ongameViewClick ); 
			m_gameBg.addEventListener(Event.ENTER_FRAME, onGameEnterFrame ); 
			
			m_gameBgMove.y = -500;
			m_sprite.addChild( m_gameBgMove );
			
			m_gril   = addChildLoaderSource( ResPath.SWF + "DragonOut.swf" );
			m_gril.x = 300;
			m_gril.y = 500;
			m_gril.scaleX = .5;
			m_gril.scaleY = .5;
			m_sprite.addChild( m_gril );
			m_girl_current_status = m_girl_on_ground;
			m_mouseEnble = false;
			
			m_stoneProduceDifficult = m_stoneProduceEasy;
			
			bgConcate(1); 
			serviceFun();
		}
		
		/**
		 * 显示界面信息
		 * 
		 */		
		private function showPopupow() : void
		{
			if( !m_TimeIsRun ){
				m_timeEvent = new Timer(1000,m_currentGateTime);
				m_timeEvent.addEventListener(TimerEvent.TIMER, onTimerFun);
			}
		}
		
		private function onTimerFun( event:TimerEvent ) : void
		{
			m_TimeIsRun = true;
			var timer:uint = m_currentGateTime - m_timeEvent.currentCount;
			timerHander( timer );
			onTimerCompleteFun( timer );
			
		}
		private function onTimerCompleteFun( timer:int ) : void
		{
			if(timer <= 0){
				if( m_dragonGoPlay.created ){
					m_dragonGoPlay.hide();
					m_TimeIsRun = false;
				}
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_MINI_GAME_OVER);
			}
		}
		/**
		 * 时间格式化工具
		 * @param time
		 * 
		 */		
		private function timerHander( time:int ):void
		{
			var mi:int  = time % 60; //秒钟 
			var minString:String ="";
			minString += mi <10 ? "0"+mi :mi.toString();
			m_dragonGoPlay.lastTimeLab.htmlText = "<font color='#00694A'>" + time + "</font>";
		}
		/**********************与服务端通信********************/
		/**
		 * 关卡
		 * @param event
		 */	
		private var m_isSourceOk:Boolean;
		private function dragonGate( event:CustomMsgEvent ) : void
		{
			m_gateArray = [];
			var playList:Array	=	event.pList;
			m_gateArray = playList;
			m_gateBoo = true;
			if(m_isSourceOk){
				PrizeFun(1);
				return;
			}
			serviceHandler();
		}
		/**
		 * 排名
		 * @param event
		 */		
		private function dragonList(e:CustomMsgEvent):void
		{
			m_listArray = [];
			var arr:Array	= e.pList;
			m_listArray = arr;
			m_listBoo = true;
			if(m_isSourceOk){
				ListFun();
				return;
			}
			serviceHandler();
		}
		
		/**
		 * 服务端消处理
		 * 
		 */		
		private function serviceHandler() : void
		{
			if(m_gateBoo&&m_listBoo){
				loadSource();      //资源
				showPopupow();     //显示界面信息
				m_gateBoo = false;
				m_listBoo = false;
				m_isSourceOk = true;
				if(m_TimeIsRun == true){
					m_dragonGoPlay.beginBtn.visible = false;
					m_gateFlag= 0;
					m_dragonGoPlay.beginBtn.visible = false;
					m_dragonGoPlay.shortBtn.enabled = false;
					m_beginGameBtn = true;
				}
			}
		}
		/**
		 * 一键奖励
		 *  
		 */		
		private function dragonPrize(e:CustomMsgEvent) : void
		{
			var arr:Array	= e.pList;
			m_dragonGoPlay.buttonPrizeLab.text = arr[0];
			m_buttonReward = arr[0];
		}
		/**
		 * 界面加载完成 
		 * 处理界面信息
		 */		
		private function serviceFun():void
		{
			PrizeFun(0);
			ListFun();
		}
		
		private function PrizeFun( flag:int ):void
		{
			if( !m_dragonGoPlay.visible ){
				m_gateFlag= 1;
				m_dragonGoPlay.visible = true;
				m_dragonGoPlay.beginBtn.visible = false;
				m_dragonGoPlay.shortBtn.enabled = false;
				return;
			}
			m_dragonGoPlay.currentGateLab.htmlText  ="<font color='#DD0D00'>" + (m_gateArray[0]).toString() + "</font>";
			m_dragonGoPlay.currentPrizeLab.htmlText ="<font color='#764500'>" + m_gateArray[1].toString() + "</font>";
			
			m_currentGate = m_gateArray[0];
			var gateMassage:String = m_gameGateObj["Lv"+m_currentGate];
			if(!gateMassage){
				m_dragonGoPlay.hide();
				return;
			}
			var gateArray:Array    = gateMassage.split(",");
			m_currentStoneNum = gateArray[0];
			if(m_TimeIsRun == false){
				if( flag == 0){
					m_currentGateTime = gateArray[1];
				}
			}
		}
		
		private function ListFun():void{
			var str:String	=	"";
			var rank:String;           //名次
			var NameStr:String;        //名字
			var scoreStr:String	=	"";//积分
			var count:int	=	1;
			var txt:TextField	=	m_showClip.getChildByName("txt5") as TextField;
			var scoreTxt:TextField	=	m_showClip.getChildByName("txt6") as TextField;
			
			txt.htmlText		=	"";
			txt.text			=	"";
			scoreTxt.htmlText	=	"";
			(txt as TextField).mouseEnabled = false;
			(txt as TextField).mouseWheelEnabled = false;
			(scoreTxt as TextField).mouseEnabled = false;
			(scoreTxt as TextField).mouseWheelEnabled = false;
			
			if( m_listArray ){
				for( var i:int= 0;i < m_listArray.length;i+=2 )
				{
					NameStr		=	m_listArray[i];
					rank		=	count.toString();
					str			=	str.concat(rank+"\t"+NameStr);
					scoreStr	=	scoreStr.concat(m_listArray[i+1]);
					if(m_listArray[i] == ModelLocator.getInstance().scene.Role.GetProperties("Name"))
					{
						str			=	"<font color='#006600'>"+str+"</font>";
						scoreStr	=	"<font color='#006600'>"+scoreStr+"</font>";
					}
					str			=	str.concat("<br>");
					scoreStr	=	scoreStr.concat("<br>");
					count++;		
				}
			}
			
			txt.htmlText		=	str;
			scoreTxt.htmlText	=	scoreStr;
			
		}
		
		/**********************游戏逻辑*********************/
		
		/**数据处理**/
		/**
		 * 下一关重置数据
		 * 
		 */		
		private function NextGateRestart():void
		{
			m_girl_current_status = m_girl_on_ground;
			moveRestart(0);
			grilStand();
			m_beginGameBtn = false;
			m_gameoverBool = false;
			m_gameoverKatoon = null;
			m_timeEvent.reset();
			m_gril.x = 10;
			m_gril.y = 500;
			m_gameBgMove.y = -500;
			m_girl_current_action = 0;
			m_oldTime    = 0;
			m_stoneIndex = 0;
			m_heightest  = 0;
			m_bg_walk    = 0;
			Restart();
			m_lastStoneArea = "";
			if( m_gameoverShow ){
				m_sprite.removeChild( m_stone_finish );
				m_gameoverShow = false;
			}
			var stone:MovieClip;
			var i:int;
			for( i = 0; i<m_bellArray.length;i++){
				stone = m_bellArray[i].stone;
				m_bellArray[i].hit = true;
				stone.y = 0;
				if(m_bellArray[i].flag == m_stone_move){
					m_sprite.removeChild( stone );
					m_bellArray[i].flag = m_stone_stop;
				}
			}
			
			for( i = 0; i<m_stone_array.length;i++ )
			{
				stone = m_stone_array[i].stone;
				stone.y = 0;
				if(m_stone_array[i].flag == m_stone_move ){
					m_gameBgMove.removeChild( stone );
					m_stone_array[i].flag = m_stone_stop;
				}
			}
			bgConcate(1);
			m_dragonGoPlay.lastTimeLab.htmlText = "<font color='#00694A'>" + "00" + "</font>";
			m_dragonGoPlay.lastHeight.htmlText = "<font color='#00694A'>" + "" + "</font>";
		}
		/**
		 * 重置数据
		 */		
		private function Restart():void
		{
			m_mouseEnble = false;
			m_allStoneFlag = false;
			m_stoneProduceDifficult = m_stoneProduceEasy;
			m_girl_current_status = m_girl_on_ground;
			m_oldTime = 0;
			m_girl_current_y = m_gril.y;
		}
		
		/**
		 * 达到一定高度后就结束游戏
		 * 
		 */		
		private function gameSuccess():void 
		{
			var height:Array = (m_gameGateObj["Lv"+m_currentGate] as String).split(",")
			var heighter:int = height[0];
			m_needHeight = heighter;
			
			if( m_bg_walk >= heighter - 400 && m_gameoverShow == false ){
				if(m_gameBgMove.y <= -300 ){
					m_stone_finish.y = -200;
					m_sprite.addChildAt( m_stone_finish ,m_sprite.numChildren);
					m_sprite.swapChildren( m_stone_finish ,m_gril );
					m_gameoverShow = true;
				}
			}
		}
		
		/**
		 * 接受石头界限判断
		 * 
		 */		
		private function finishStone():void
		{
			if( m_gameoverShow ){
				if(m_stone_finish.y <-400|| m_stone_finish.y > 1500)
				{
					m_sprite.removeChild( m_stone_finish );
					m_gameoverShow = false;
				}
				var mv:MovieClip = m_gril.getChildByName( "btn" ) as MovieClip;
				var mv1:MovieClip = m_stone_finish.getChildByName("btn") as MovieClip
				if( mv.hitTestObject( mv1 )&&m_gameoverKatoon == null )
				{
					m_gril.x = 450;
					m_girl_current_status = m_girl_on_ground;
					moveRestart(0);
					grilStand();
					m_beginGameBtn = false;
					m_timeEvent.stop();
					var stone:MovieClip;
					var i:int;
					for( i = 0; i<m_bellArray.length;i++){
						stone = m_bellArray[i].stone;
						m_bellArray[i].hit = true;
						stone.y = 0;
						if(m_bellArray[i].flag == m_stone_move){
							m_sprite.removeChild( stone );
							m_bellArray[i].flag = m_stone_stop;
						}
					}
					
					var	yesBtnT	:String			=	StaticIni.getSwfLangStr("SellUI_Sure");
					var aa:String = StaticIni.getSwfLangStrVar("UIString_1000604",[ m_currentGate, 
						int((int(m_dragonGoPlay.lastTimeLab.text))/3)]) ;
					AlertUI.showAlertEx("",aa, [yesBtnT],AlertUI.YES ,clickClearCD);
					
					//=============
					
				}
			}
		}
		
		private function  clickClearCD( type:int, str:String, clickData:* ):void
		{
			if(AlertUI.YES == type )
			{
				m_gameoverKatoon= addChildLoaderSource( ResPath.SWF + "switchScene1.swf" );
				m_gameoverKatoon.scaleX = .5;
				m_gameoverKatoon.scaleY = .5;
				m_sprite.addChildAt( m_gameoverKatoon, m_sprite.numChildren);
				m_gameoverKatoon.gotoAndPlay(1);
				m_gameoverBool = true;
				var a:DisplayObject =  m_sprite.getChildByName("m_start_Katoon" );
				if( a ){
					m_sprite.removeChild(m_start_Katoon);
				}else{
					trace("null" + a);
				}
				m_startKaton = false;
			}
		}
		/**
		 * 游戏结束播放动画
		 * 
		 */		
		private function gameoverKatoon() : void
		{
			if( m_gameoverBool ){
				if( m_gameoverKatoon.currentFrame == m_gameoverKatoon.totalFrames )
				{
					m_sprite.removeChild( m_gameoverKatoon );
					m_currentGate += 1 ;
					if( !m_gameGateObj["Lv"+m_currentGate] ){    //通关判断
						m_dragonGoPlay.showOrHide();
						return;
					}
					var gateMassage:String = m_gameGateObj["Lv"+m_currentGate];
					var gateArray:Array    = gateMassage.split(",");
					m_currentGateTime = 0;
					m_currentGateTime = int(gateArray[1]) + (int(m_dragonGoPlay.lastTimeLab.text))/3;
					SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENR_CUSTOMMSG_DRAGON_SUCCESS,m_currentGateTime);
					m_dragonGoPlay.currentGateLab.htmlText  ="<font color='#DD0D00'>" + m_currentGate.toString() + "</font>";
					NextGateRestart();
					m_gateKatoon = true;
				}
			}
		}
		
		/**
		 * 运动状态数据重置
		 * 0初始化数据 1 是跑步状态 2是站立状态 3 向上 4 向下 5跳 6落地
		 */		
		private function moveRestart( moveStatus:int ) : void
		{
			if( moveStatus != 1 ){
				m_gril_Runing = false;
			}
			if(moveStatus != 2){
				m_gril_standStart = false;
			}
			if(moveStatus != 3){
				m_gril_Up = false;
			}
			if(moveStatus != 4){
				m_gril_down = false;
			}
			if(moveStatus != 5){
				m_gril_jump = false;
			}
			if(moveStatus != 6){
				m_gril_Trace = false;
			}
		}
		
		/**游戏运动中**/
		/**
		 * 每一帧游戏的动态
		 * @param event
		 */		
		private function onGameEnterFrame( event:Event ) : void
		{
			var mc:MovieClip = event.currentTarget as MovieClip;
			gameoverKatoon();
			hasTimeListener();
			gamestartKatoon(m_gateFlag);
			if(m_beginGameBtn == true){
				grilmove( mc.mouseX , mc.mouseY );
				stonedown();
				hitTest();
				gameSuccess();
				gameDiffent();
				finishStone();
				var height:int = m_needHeight - m_bg_walk ;
				if(height <= 0){
					height = 0;
				}
				m_dragonGoPlay.lastHeight.htmlText =height+ "";
			}else{
				startGrilMove( mc.mouseX );
			}
		}
		/**
		 * 判断时间是否再走
		 * 
		 */		
		private function hasTimeListener():void
		{
			if( !m_timeEvent.running )
			{
				if( m_beginGameBtn == true ){
					if(m_timeEvent.hasEventListener(TimerEvent.TIMER)){
						if( m_currentGateTime > 0){
							m_timeEvent.start();
							m_dragonGoPlay.shortBtn.enabled = false;
						}
					}else{
						m_timeEvent.addEventListener(TimerEvent.TIMER, onTimerFun);
					}
				}
			}else{
				if( m_dragonGoPlay.shortBtn.enabled ){
					m_dragonGoPlay.shortBtn.enabled = false;
				}
			}
		}
		
		/**
		 * 播放开始动画
		 * 1点击 2为龙女过场动画
		 */		
		private function gamestartKatoon( flag:int ):void
		{
			if(m_gateFlag != 0){
				if(m_startKaton == false){
					
					m_start_Katoon.movie1.num.text = StaticIni.getSwfLangStrVar("UIString_1000643",[m_currentGate]);
					m_start_Katoon.x = 120;
					m_start_Katoon.y = 200;
					m_sprite.addChild(m_start_Katoon);
					m_start_Katoon.gotoAndPlay(1);
					m_startKaton = true;
				}
				if( m_startKaton == true ){
					if(m_start_Katoon.currentFrame == m_start_Katoon.totalFrames)
					{
						m_gateKatoon = false;
						m_sprite.removeChild(m_start_Katoon);
						m_startKaton = false;
						if( flag == 2){
							m_beginGameBtn = true;
							moveRestart(0);
							m_timeEvent.start();
						}else{
							m_beginGameBtn = true;
							m_dragonGoPlay.beginBtn.visible = false;
							m_timeEvent.start();
						}
						m_gateFlag = 0;
					}
				}
			}
		}
		
		/**
		 *开始游戏龙女过场 
		 * 
		 */		
		private function startGrilMove( mousex:int ):void
		{
			if( m_dragonGoPlay.beginBtn.visible != true ){
				if(m_gril.x < 300){
					m_gril.x += 5;
					m_gril.rotationY = 0;
					grilTurning( -1 );
				}else{
					if(m_gateKatoon){
						m_gateFlag = 2;
						grilStand();
					}
				}
			}
		}
		
		/**
		 * 游戏难度
		 * 
		 */		
		private function gameDiffent():void
		{
			var arr:Array;
			switch(m_stoneProduceDifficult)
			{
				case m_stoneProduceEasy:
					arr = (m_stoneAreaObjeasy["Rank"] as String).split(",");
					if(m_bg_walk > arr[1]){
						m_stoneProduceDifficult = m_stoneProducesimplame;
					}
					m_createTime = int(m_stoneAreaObjeasy["Space"]);
					break;
				case m_stoneProduceHand:
					arr = (m_stoneAreaObjhand["Rank"] as String).split(",");
					if(m_bg_walk > arr[1]){
						m_stoneProduceDifficult = m_stoneProduceHand;
					}
					if(m_bg_walk < arr[0]){
						m_stoneProduceDifficult = m_stoneProducesimplame;
					}
					m_createTime = int(m_stoneAreaObjeasy["Space"]);
					break;
				case m_stoneProducesimplame:
					arr = (m_stoneAreaObjsimp["Rank"] as String).split(",");
					if(m_bg_walk > arr[1]){
						m_stoneProduceDifficult = m_stoneProduceHand;
					}
					if(m_bg_walk < arr[0]){
						m_stoneProduceDifficult = m_stoneProduceEasy
					}
					m_createTime = int(m_stoneAreaObjeasy["Space"]);
					break
			}
			
		}
		/**
		 * 龙女和石头的碰撞
		 */		
		private function hitTest():void
		{
			var stone:MovieClip;
			for( var i:int = 0; i<m_bellArray.length;i++){
				stone = m_bellArray[i].stone;
				var mv:MovieClip = m_gril.getChildByName( "btn" ) as MovieClip;
				if( mv.hitTestObject( stone )&&m_bellArray[i].hit == true){
					if(m_bellArray[i].flag == m_stone_move){
						stone.gotoAndPlay("play");
						m_bellArray[i].hit = false;
					}
					m_oldTime = 0;
					m_girl_current_y = m_gril.y;
					m_girl_current_action = m_girl_jump;
					grilJump();
					m_grilHitStoneNum ++;
				}
			}
		}
		/**
		 * 点击游戏界面 
		 * @param event
		 * 
		 */		
		private function ongameViewClick( event:MouseEvent ) : void
		{
			if( !m_mouseEnble ){
				m_mouseEnble = true;                    //鼠标失效
				m_girl_current_status = m_girl_up_sky;
				m_girl_current_action = m_girl_jump;
				m_girl_current_y = m_gril.y;
			}
		}
		/**
		 * 龙女移动
		 * @param mousex
		 * @param mousey
		 */		
		private function grilmove( mousex:Number,mousey:Number ) : void
		{
			grilRun( mousex );
			if( m_girl_current_status == m_girl_on_ground ){ //小球是否在地面
				m_heightest = 0;
				girlXmove( mousex );
			}else if( m_girl_current_status == m_girl_up_sky ){
				ballOnskymove( mousex );
				ballonSkyYmove();
			}
		}
		
		/**
		 * 石头下落
		 * 
		 */		
		private function stonedown():void
		{
			var stone:MovieClip;
			if( !m_bellArray )return;
			for( var i:int = 0; i<m_bellArray.length;i++){
				if( m_bellArray[i].flag == m_stone_stop)continue;
				stone = m_bellArray[i].stone as MovieClip;
				stone.y += 3;
				
				if(m_bellArray[i].hit == false){
					if(stone.currentFrame == stone.totalFrames){
						m_bellArray[i].hit = true;
						m_bellArray[i].flag = m_stone_stop;
						stone.y = 0;
						m_sprite.removeChild( stone );
					}
				}
				if( m_bellArray[i].flag == m_stone_stop)continue;
				if( m_girl_current_status == m_girl_on_ground){
					if( stone.y >= 400 )
					{
						m_bellArray[i].flag = m_stone_stop;
						stone.y = 0;
						m_sprite.removeChild( stone );
					}
				}else{
					if( stone.y >= 500 )
					{
						m_bellArray[i].flag = m_stone_stop;
						stone.y = 0;
						m_sprite.removeChild( stone );
					}
				}
			}
			stoneBornHandler();
		}
		
		
		
		/*********石头的生成处理*******/
		/**
		 * 通过高度来生成石头
		 * 定值 100为判断 20是石头的初始位置
		 */		
		
		private function stoneBornHandler():void
		{
			var stone:MovieClip;
			if(m_bellArray.length != 0){
				stone = m_bellArray[m_stoneIndex].stone;
				if(stone.y >= m_createTime){
					var size:int = stone.y - m_createTime -20;
					stoneBorn( size );
				}
				if(m_bellArray[m_stoneIndex].flag == m_stone_move)
				{
					m_allStoneFlag = true;
				}else{
					m_allStoneFlag = false;
				}
			}else{
				stoneBorn( -20 );
				m_allStoneFlag = true;
			}
			
			if(m_bellArray.length != 0){  //检查界面是否有石头 如果没有石头就生成
				if( m_allStoneFlag == false)
				{
					stoneBorn( -20 );
					m_allStoneFlag = false;
				}
			}
		}
		
		/**
		 * 石头生成器
		 */		
		private function stoneBorn( localY:int ):void
		{
			var stone:MovieClip;
			for(var i:int = 0;i< m_bellArray.length;i++){   //检查是否有可用的石头
				var flag:int = m_bellArray[i].flag;
				if( flag == m_stone_stop ){
					m_bellArray[i].flag = m_stone_move;
					m_bellArray[i].hit = true;
					stone = m_bellArray[i].stone;
					stoneCreateLocal(stone);
					stone.y = localY;
					m_sprite.addChildAt( stone , m_sprite.numChildren -1);
					m_stoneIndex = i;
					return;
				}
			}
			
			stone =  new m_stoneClass();
			stoneCreateLocal(stone);
			stone.y = localY;
			stone.scaleX = .6;
			stone.scaleY = .6;
			m_sprite.addChild( stone );
			var bellobj:Object = {"stone":stone,"flag":m_stone_move,"hit":true};
			m_bellArray.push( bellobj );
			m_stoneIndex = m_bellArray.length - 1;
		}
		
		/**
		 * 石头生成的位置
		 * @param stone
		 * 通过但前的难度来判断
		 */		
		private function stoneCreateLocal( stone:MovieClip ) : void
		{
			var areaName:String = girlCurrentArea();
			var i:String
			switch( m_stoneProduceDifficult )
			{
				case m_stoneProduceEasy:
					for( i in m_stoneAreaObjeasy){
						if( areaName == i){
							stoneCreateHandler(m_stoneAreaObjeasy[i], stone);
						}
					}
					break;
				case m_stoneProducesimplame:
					for(  i in m_stoneAreaObjsimp){
						if( areaName == i){
							stoneCreateHandler(m_stoneAreaObjsimp[i], stone);
						}
					}
					break;
				case m_stoneProduceHand:
					for(  i in m_stoneAreaObjhand){
						if( areaName == i){
							stoneCreateHandler(m_stoneAreaObjhand[i], stone);
						}
					}
					break;
			}
			
		}
		
		/**
		 * 当前龙女所在的区域名字 
		 * @return 
		 * 
		 */		
		private function girlCurrentArea():String
		{
			if( m_stoneArea_1[0]<=m_gril.x&&m_gril.x<= m_stoneArea_1[1])return "a";
			if( m_stoneArea_2[0]<m_gril.x&&m_gril.x<= m_stoneArea_2[1])return "b";
			if( m_stoneArea_3[0]<m_gril.x&&m_gril.x<= m_stoneArea_3[1])return "c";
			if( m_stoneArea_4[0]<m_gril.x&&m_gril.x<= m_stoneArea_4[1])return "d";
			if( m_stoneArea_5[0]<m_gril.x&&m_gril.x<= m_stoneArea_5[1])return "e";
			return "a";
		}
		
		/**
		 * 石头的生成位置
		 * @param str
		 * 
		 */		
		private function stoneCreateHandler( str:String ,stone:MovieClip) : void
		{
			var ramNum:int;
			var areaname:String; 
			var strArrray:Array = str.split(",");
			var choice:Boolean = true;
			
			while( choice ){
				ramNum = 1 + strArrray.length*Math.random();
				areaname = strArrray[ramNum-1];
				if( strArrray.length ==1 )choice = false;
				if( areaname != m_lastStoneArea) choice = false;
			}
			
			switch( areaname)
			{
				case "a":
					stone.x = m_stoneArea_1[0] + 96*Math.random();
					break;
				case "b":
					stone.x = m_stoneArea_2[0] + 96*Math.random();
					break;
				case "c":
					stone.x = m_stoneArea_3[0] + 96*Math.random();
					break;
				case "d":
					stone.x = m_stoneArea_4[0] + 96*Math.random();
					break;
				case "e":
					stone.x = m_stoneArea_5[0] + 96*Math.random();
					break;
			}
			m_lastStoneArea = areaname;
		}
		
		
		
		/**************龙女处理***********/
		/**
		 * 边界预估值判断  
		 * @return 
		 */		
		private function LimitJudge( mousex:int ,girlXspeed:int,flag:int):Boolean
		{
			var speed:int;
			var limitX:int
			if( flag == 1){
				speed = m_gril.x < mousex ? 1 : -1;
				limitX = m_gril.x + m_girl_onground_speed*speed; //预估值
			}else{
				limitX = girlXspeed;
			}
			if( limitX < 60){
				m_gril.x = 60;
				m_gril_standStart = false;
				grilStand();
				return false;
			} 
			else if( limitX > 540){
				m_gril.x = 540;
				m_gril_standStart = false;
				grilStand();
				return false;
			}
			
			return true;
		}
		
		
		/**
		 * 小球y轴方向的运动
		 * 储存小球的新旧位置
		 */		
		private function ballonSkyYmove():void
		{
			
			if(m_gril.y > 500){ //y边界判断
				m_gril.y = 500;
				grilTrace();
				Restart();
				return ;
			}
			
			m_oldTime++;
			if(m_oldTime == 1){
				m_girl_old_y = m_gril.y;
			}else{
				m_girl_old_y = m_girl_new_y;
			}
			//a除以4 b处以2 比率
			m_girl_new_y = 1.5*m_oldTime*m_oldTime -30*m_oldTime + m_girl_current_y ;
			var walk:int = m_girl_old_y - m_girl_new_y;
			var intendNum:int   = m_girl_new_y; //预估值
			if( walk >= 0 )  //向上
			{
				grilJump();
				bgUpMove( intendNum ,walk );
			}else if( walk < 0 ){ //向下
				grilDown();
				if(m_oldTime < 35){  //到达一定时间开始加速
					bgDownMove( -20 );
				}else{
					bgDownMove( walk );
				}
			}
		}
		
		/**********背景移动处理*********/
		
		/**
		 * 背景的配合
		 * 1初始背景 2向上运动背景拼合 3向下
		 * m_gameNumStone 游戏中最多存在两个石头 留一个空余的石头替补
		 */		
		
		private function bgConcate( flag:int,move:int=0) : void
		{
			var stone:MovieClip;
			var i:int;
			m_flag =0;
			
			if( flag == 1) //背景的拼合
			{
				m_gameNumStone = 0;
				for(  i= 0;i < m_stone_array.length;i++){
					stone = m_stone_array[i].stone ;
					if(m_gameNumStone == 2)continue;
					if(m_stone_array[i].name == "three"){
						stone.y = 500;
						m_gameNumStone++;
					}else if(m_stone_array[i].name == "one"){
						stone.y = 0;
						m_gameNumStone++;
					}
					if(m_stone_array[i].flag == m_stone_stop ){
						m_gameBgMove.addChild( stone );
						m_stone_array[i].flag = m_stone_move;
					}
				}
			}
			if(flag == 2){
				for(  i = 0;i < m_stone_array.length;i++){
					stone = m_stone_array[i].stone ;
					if( m_gameNumStone == 1){
					}
					if(m_flag <= 1){                              //每次进来只执行一次
						if(m_stone_array[i].flag == m_stone_move) //针对于即将移出舞台背景
						{
							if( stone.y >= 490){
								m_gameBgMove.removeChild( stone );
								m_gameNumStone--;
								m_stone_array[i].flag = m_stone_stop;
								m_flag++;
							}else if(stone.y <= 10){
								stone.y = 500;
								m_flag++;
								
							}
						}
					}
					
					if(m_stone_array[i].flag == m_stone_stop&&m_gameNumStone <= 1){ //针对于即将下移背景
						if(m_bg_walk > 400){
							if(m_stone_array[i].name == "three")continue;
							stone.y = 0;
							m_gameBgMove.addChild( stone );
							m_gameNumStone++;
							m_stone_array[i].flag = m_stone_move;
						}else{
							if(m_stone_array[i].name == "three") //针对于地面背景
							{
								m_gameBgMove.addChild( stone );
								m_gameNumStone++;
								m_stone_array[i].flag = m_stone_move;
							}
						}
					}
				}
			}
			if(flag == 3){
				for(  i = 0;i < m_stone_array.length;i++){
					stone = m_stone_array[i].stone ;
					if(m_flag <= 1){                              //每次进来只执行一次
						if(m_stone_array[i].flag == m_stone_move) //针对于即将移出舞台背景
						{
							if( stone.y <= 10){
								m_gameBgMove.removeChild( stone );
								m_gameNumStone--;
								m_stone_array[i].flag = m_stone_stop;
								m_flag++;
							}else if( stone.y >= 490){
								stone.y = 0;
								m_flag++;
								
							}
						}
					}
					if(m_bg_walk > 450){
						if(m_stone_array[i].flag == m_stone_stop &&m_gameNumStone <= 1){ //针对于即将shang移背景
							if(m_stone_array[i].name == "three")continue;
							stone.y = 500;
							m_gameBgMove.addChild( stone );
							m_gameNumStone++;
							m_stone_array[i].flag = m_stone_move;
						}
					}else{
						if(m_stone_array[0].flag == m_stone_stop&&m_gameNumStone == 1){
							m_stone_array[0].stone.y = 500;
							m_gameBgMove.addChild( m_stone_array[0].stone );
							m_gameNumStone++;
							m_stone_array[0].flag = m_stone_move;
						}
						continue;
					}
				}
			}
			
			
			//检查左右石头的状态
			var showFurone:Boolean;
			var showFurtwo:Boolean;
			
			for(  i = 0;i < m_stone_array.length;i++){
				stone = m_stone_array[i].stone ;
				if(m_stone_array[i].flag == m_stone_move) //针对于即将移出舞台背景
				{
					if(stone.y == 0){
						if(showFurone == true){
							m_gameBgMove.removeChild( stone );
							m_stone_array[i].flag = m_stone_stop;
							continue;
						}
						showFurone = true;
					}else if( stone.y == 500){
						if(showFurtwo == true){
							m_gameBgMove.removeChild( stone );
							m_stone_array[i].flag = m_stone_stop;
							continue;
						}
						showFurtwo = true;
					}
				}
			}
			
			if(showFurone&&showFurtwo){
			}else{
				if( showFurone == false ){
					if(m_stone_array[1].flag == m_stone_stop){
						m_stone_array[1].stone.y = 0;
						m_gameBgMove.addChild( m_stone_array[1].stone );
						m_stone_array[1].flag = m_stone_move;
					}else if(m_stone_array[2].flag == m_stone_stop){
						m_stone_array[2].stone.y = 0;
						m_gameBgMove.addChild( m_stone_array[2].stone );
						m_stone_array[2].flag = m_stone_move;
					}else if(m_stone_array[3].flag == m_stone_stop){
						m_stone_array[3].stone.y = 0;
						m_gameBgMove.addChild( m_stone_array[3].stone );
						m_stone_array[3].flag = m_stone_move;
					}
				}else if(showFurtwo == false){
					if(m_stone_array[1].flag == m_stone_stop){
						m_stone_array[1].stone.y = 500;
						m_gameBgMove.addChild( m_stone_array[1].stone );
						m_stone_array[1].flag = m_stone_move;
					}else if(m_stone_array[2].flag == m_stone_stop){
						m_stone_array[2].stone.y = 500;
						m_gameBgMove.addChild( m_stone_array[2].stone );
						m_stone_array[2].flag = m_stone_move;
					}else if(m_stone_array[3].flag == m_stone_stop){
						m_stone_array[3].stone.y = 500;
						m_gameBgMove.addChild( m_stone_array[3].stone );
						m_stone_array[3].flag = m_stone_move;
					}
				}
			}
			if( m_heightest >= 500 && m_bg_walk > 500 ){
				if(m_stone_array[0].flag == m_stone_move){
					m_gameBgMove.removeChild( m_stone_array[0].stone );
					m_stone_array[i].flag = m_stone_stop;
				}
			}
			
		}
		
		/**
		 * 背景移动有三个状态 未到达 已到达 要到达
		 * @param intendNum 预估值
		 * @param walk      走的长度
		 * //三种情况 未到达 已到达 要到达  
		 */		
		private function bgUpMove( intendNum :int ,walk:int):void
		{
			var intendNumMax:int = Math.floor(m_girl_higher - intendNum); //预估值
			
			/**小球上升过程中背景的三种可能性**/
			
			if( intendNumMax < 0) //未到达
			{
				m_gril.y = m_girl_new_y;
			}
			
			if( intendNumMax > 0 ) //包含两种情况 已到达 和 要到达
			{
				if( m_gril.y == m_girl_higher )  //已到达
				{
					m_gril.y = m_girl_higher;
					bgDownMoveJume( walk );
					m_gameBgMove.y  += walk;
					stoneMoveFun(walk);
					m_bg_walk += walk;
					if( m_gameoverShow ){
						m_stone_finish.y += walk;
					}
				}else if( m_gril.y > m_girl_higher ){ //要到达
					m_gril.y = m_girl_higher;
					bgDownMoveJume( intendNumMax );
					m_gameBgMove.y += intendNumMax;
					stoneMoveFun(intendNumMax);
					m_bg_walk += intendNumMax;
					if( m_gameoverShow ){
						m_stone_finish.y += intendNumMax;
					}
				}
				m_heightest = m_bg_walk;
			}
			
		}
		
		
		/**
		 * 背景下落中接环判断
		 * @param intendNumMax
		 * @return 
		 * 
		 */		
		private function bgDownMoveJume( intendNumMax:int ) : void
		{
			var intendNumbg:int;                               //背景移动预估值
			intendNumbg = m_gameBgMove.y + intendNumMax;
			if( intendNumbg >= 0 ){
				bgConcate(2,-500);
				m_gameBgMove.y = -500;
			}
		}
		
		/**
		 * 和背景同步移动的石头们
		 * @param move
		 */		
		private function stoneMoveFun( move:int ):void
		{
			var stone:MovieClip;
			for(var i:int = 0;i< m_bellArray.length;i++){   //检查石头是否移除边界
				var flag:int = m_bellArray[i].flag;
				if( flag == m_stone_move ){
					stone =  m_bellArray[i].stone;
					stone.y += move;
					if(stone.y < -50){
						stone.y = 0;
						m_sprite.removeChild( stone );
						m_bellArray[i].flag = m_stone_stop;
					}
				}
			}
			stoneBornHandler();
		}
		/**
		 * 背景shang移  
		 * @param walk
		 * 未到达 已到达 要到达 背景是否已经到地面了
		 */		
		private function bgDownMove( walk:int ):void
		{
			var intendNumMin:int = Math.floor( m_girl_lower - m_gril.y); //预估值
			var intenderNum:int;                                         //下落预估值
			
			/**小球下降过程中4种可能性**/
			if( intendNumMin > 0) //未到达
			{
				var intendNewMin:int = -(-m_gril.y + walk);
				if(m_bg_walk > 0){  //背景已经到地面了
					if( intendNewMin > m_girl_lower ){
						m_gril.y = m_girl_lower;
						bgUpmoveJume(intendNumMin);
						m_gameBgMove.y += intendNumMin;
						stoneMoveFun(intendNumMin);
						m_bg_walk -= intendNumMin;
						if( m_gameoverShow ){
							m_stone_finish.y += intendNumMin;
						}
						return;
					}
				}
				m_gril.y -= walk;
			}
			
			if(intendNumMin <= 0) //已到达 和 要到达
			{
				if(m_bg_walk > 0){  //背景已经到地面了
					if( m_gril.y == m_girl_lower ) //已到达
					{
						m_gril.y = m_girl_lower;
						intenderNum = m_bg_walk + walk;
						bgUpmoveJume(walk);
						m_gameBgMove.y  += walk;
						stoneMoveFun(walk);
						m_bg_walk += walk;
						if( m_gameoverShow ){
							m_stone_finish.y += walk;
						}
					}else if( m_gril.y < m_girl_lower ){
						m_gril.y   = m_girl_lower;
						bgUpmoveJume(intendNumMin);
						m_gameBgMove.y += intendNumMin;
						stoneMoveFun(intendNumMin);
						m_bg_walk  += intendNumMin;
						if( m_gameoverShow ){
							m_stone_finish.y += intendNumMin;
						}
					}
				}else{
					m_gril.y -= walk;
				}
				
				if(m_heightest > 300){
					if(m_bg_walk <= 100){ //误差值   掉落地面数据刷新
						m_bg_walk = 0;
						m_gril.y = 485;
						m_gameBgMove.y = -500;
						bgConcate(1);
						m_heightest = 0;
						moveRestart(0);
						grilStand();
					}
				}
				if( m_bg_walk <= 0 )
				{
					m_gameBgMove.y = -500;
					m_bg_walk = 0;
				}
				
			}
		}
		/**
		 * 背景上升预估值 
		 * 
		 */		
		private function bgUpmoveJume(intendNumMin:int):void
		{
			var intenderNum:int;  
			intenderNum = m_gameBgMove.y + intendNumMin;
			if( intenderNum <= -490 &&m_heightest>400){
				bgConcate(3,500);
				m_gameBgMove.y = 0;
			}
		}
		
		
		/**
		 * 龙女在控制运动的x轴状态
		 * @param mousex
		 */		
		private function ballOnskymove( mousex:int ):void
		{
			var speed:int = m_gril.x < mousex ? 1 : -1;
			var dx:int = (mousex - m_gril.x) / m_girl_onsky_speed;
			var future:int = m_gril.x + dx;
			if( !LimitJudge( mousex ,future ,2) )return;      //预估值
			m_gril.x += dx;
		}
		
		/**
		 * 龙女在地面运动情况
		 * @param key
		 */		
		private function girlXmove( mousex:Number ):void
		{
			if( !LimitJudge( mousex ,m_girl_onground_speed, 1) )return;
			if( m_gril.x + 2 >= mousex&&m_gril.x - 2 <= mousex ){
				grilStand();
				return;
			}
			var speed:int = m_gril.x < mousex ? 1 : -1;
			grilTurning( speed );
			m_gril.x += m_girl_onground_speed * speed;
		}
		
		
		/**************动作提取***********/
		/**
		 * 龙女方向
		 * @param run run 1向右 -1 向左
		 */		
		private function grilRun( mousex:int ):void
		{
			var speed:int = m_gril.x < mousex ? 1 : -1;
			if( speed == 1){
				m_gril.rotationY = 0;
			}else{
				m_gril.rotationY = 180;
			}
		}
		
		/**
		 * girl的在地面的左右方向
		 * @param run 1向右 -1 向左
		 * m_gril_Runing false 还是开始跑 true已经开始跑了 0默认之前的状态
		 */		
		private function grilTurning( run:int ) :void
		{
			
			if( m_gril_Runing == false){
				m_gril.gotoAndPlay( m_gril_stutas_run );
				m_gril_Runing = true;
				moveRestart(1);
			}
		}
		
		
		/**
		 * 龙女站立待机  
		 * @param run
		 * m_gril_standStart 限制动画只播放一次
		 */		
		private function grilStand(  ) : void
		{
			if( m_gril_standStart == false &&m_girl_current_status == m_girl_on_ground )
			{
				m_gril.gotoAndPlay( m_gril_stutas_stand );
				m_gril_standStart = true;
				moveRestart(2);
			}
		}
		
		
		/**
		 * 龙女向上跳
		 * 
		 */		
		private function grilUp( ) : void
		{
			if( m_gril_Up == false )
			{
				m_gril.gotoAndPlay( m_gril_stutas_flyup );
				m_gril_Up = true;
				moveRestart(3);
			}
		}
		/**
		 * 龙女向下降
		 * 
		 */		
		private function grilDown( ) : void
		{
			if(m_bg_walk == 0){
				grilStand();
				return;
			}
			if( m_gril_down == false )
			{
				m_gril.gotoAndPlay( m_gril_stutas_flydown );
				m_gril_down = true;
				moveRestart(4);
			}
		}
		
		/**
		 * 龙女跳
		 * 
		 */		
		private function grilJump( ) : void
		{
			if( m_gril_jump == false && m_girl_current_action == m_girl_jump )
			{
				m_gril.gotoAndPlay( m_gril_stutas_jump );
				m_gril_jump = true;
				moveRestart(5);
				m_girl_current_action = m_girl_uping;
			}
			if( m_gril.currentLabel == m_gril_stutas_runStop)
			{
				grilUp();
			}
		}
		
		/**
		 * 龙女着路
		 * 
		 */		
		private function grilTrace( ) : void
		{
			
			if( m_gril_Trace == false )
			{
				m_gril.gotoAndPlay( m_gril_stutas_trace );
				m_gril_Trace = true;
				moveRestart(6);
			}
		}
	}
}